I try to learn GraphQL. I wanted to try out the code that is in the Getting Started With GraphQL.js section in the graphql.org. I created a server.js file like this page says: https://graphql.org/graphql-js/
I followed instructions like:
npm init
npm install graphql --save
The package.json file looks like this:
{
"name": "graphql",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"graphql": "^16.0.1"
}
}
The server.js file I created looks exactly like the one from the page (it was copied):
var { graphql, buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
hello: String
}
`);
// The root provides a resolver function for each API endpoint
var root = {
hello: () => {
return 'Hello world!';
},
};
// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
console.log(response);
});
But after using the node server.js command (as written on the page) in the terminal, I get this error:
node_modules\graphql\type\schema.js:35
throw new Error(
^
Error: Expected undefined to be a GraphQL schema.
at assertSchema (F:\GraphQL\node_modules\graphql\type\schema.js:35:11)
at validateSchema (F:\GraphQL\node_modules\graphql\type\validate.js:34:28)
at graphqlImpl (F:\GraphQL\node_modules\graphql\graphql.js:52:64)
at F:\GraphQL\node_modules\graphql\graphql.js:21:43
at new Promise (<anonymous>)
at graphql (F:\GraphQL\node_modules\graphql\graphql.js:21:10)
at Object.<anonymous> (F:\GraphQL\server.js:18:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
Does anyone know what the problem is? Maybe I am missing some package? Or the code given on the page is wrong? Please help me, thank you in advance for any answer.